public class CaseStdFieldControl {
  
        public static final integer PICKLIST_STATUS = 1;
        public static final integer PICKLIST_PRIORITY = 2;
        public static final integer PICKLIST_ORIGIN = 3;
        
        private static Map<integer, string> mapCasePicklistDefaults;
    
        public static Map<integer, string> GetDefaultPicklistValueFromCase() {
            if (CaseStdFieldControl.mapCasePicklistDefaults != null) 
                return CaseStdFieldControl.mapCasePicklistDefaults;
            
            Map<integer, string> defaultPicklistValues = new Map<integer, string>();
            List<Schema.PicklistEntry> P = Case.Priority.getDescribe().getPicklistValues();
            List<Schema.PicklistEntry> O = Case.Origin.getDescribe().getPicklistValues();
            List<Schema.PicklistEntry> S = Case.Status.getDescribe().getPicklistValues();
            for(integer i=0; i < P.size(); i++)
                { if (i==0 || P[i].isDefaultValue()) defaultPicklistValues.put( PICKLIST_PRIORITY, P[i].getLabel() ); }
            for(integer i=0; i < S.size(); i++)
                { if (i==0 || S[i].isDefaultValue()) defaultPicklistValues.put( PICKLIST_STATUS, S[i].getLabel() ); }
            for(integer i=0; i < O.size(); i++)
                { if (i==0 || O[i].isDefaultValue()) defaultPicklistValues.put( PICKLIST_ORIGIN, O[i].getLabel() ); }
            CaseStdFieldControl.mapCasePicklistDefaults = defaultPicklistValues;
            return defaultPicklistValues;
        }

        static Set<String> list2Set(List<String> lst) {
            Set<String> retSet = new Set<String>();
            retSet.addAll( lst );
            return retSet;
        }
        
        public static testMethod void testGetDefaultPicklistValueFromCaseMap() {
            Map<integer, string> mapPicklists = GetDefaultPicklistValueFromCase();
            Set<String> statusesSet = list2Set( CaseStdFieldControl.GetAllPicklistValuesFromCase('STATUS') );
            System.assert( statusesSet.contains( mapPicklists.get(PICKLIST_STATUS)) );
            Set<String> priorsSet = list2Set( CaseStdFieldControl.GetAllPicklistValuesFromCase('PRIORITY') );
            System.assert( priorsSet.contains( mapPicklists.get(PICKLIST_PRIORITY)) );
            Set<String> originsSet = list2Set( CaseStdFieldControl.GetAllPicklistValuesFromCase('ORIGIN') );
            System.assert( originsSet.contains( mapPicklists.get(PICKLIST_ORIGIN)) );
            System.assert( mapPicklists.remove(PICKLIST_PRIORITY).length() > 0 );
            System.assert( mapPicklists.remove(PICKLIST_STATUS).length() > 0 );
            System.assert( mapPicklists.remove(PICKLIST_ORIGIN).length() > 0 );
            System.assert( mapPicklists.isEmpty() );
        }
    
        //
        // Returns all Picklist values for either Case.Status or Case.Priority or Case.Origin
        //
        public static String[] GetAllPicklistValuesFromCase(String FieldName){
            
            List<String> retVal = new List<String>();
            Schema.DescribeFieldResult F;
            if(FieldName.toLowerCase() == 'status'){ 
                F = Case.Status.getDescribe();
            }
            else if(FieldName.toLowerCase() == 'priority'){
                F = Case.Priority.getDescribe();
            }
            else if(FieldName.toLowerCase() == 'origin'){
                F = Case.Origin.getDescribe();
            }
            List<Schema.PicklistEntry> P = F.getPicklistValues();
            for(Schema.PicklistEntry sp : P){
                retVal.add( sp.getLabel() );
            }
    
            return retVal; 
        } 
        
        public static Id getBusinessHours(Case c) { return c.BusinessHoursId; }
    
        public static Boolean setBusinessHours(Case c, Id bhId){
            try { c.BusinessHoursId = BHWithFallback.getSpecifiedOrDefault(bhId);}
            catch(Exception e) { return false; }
            return true;
        }
        
        public static testMethod void testSetGetBusinessHours() {
            try {
                Case c = new Case();
                Id bidInit = [select Id from BusinessHours where IsDefault=True Limit 1].Id;
                Boolean b = setBusinessHours(c,bidInit);
                system.assert(b);
                Id bid = getBusinessHours(c);
                system.assert(bid==bidInit);
            } catch (Exception e) {
                System.debug('Caught unexcepted exception -- check org setup'+e.getMessage());
            }
        }
    

          
   }